package com.seawaterbt.ssm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.seawaterbt.ssm.config.DataSource;
import com.seawaterbt.ssm.config.DataSourceEnum;
import com.seawaterbt.ssm.entity.BasHousehold;
import com.seawaterbt.ssm.entity.CzBasHeatstation;
import com.seawaterbt.ssm.entity.ShengBasHousehold;
import com.seawaterbt.ssm.mapper.ShengBasHouseholdMapper;
import com.seawaterbt.ssm.service.CzBasHouseholdService;
import com.seawaterbt.ssm.service.ShengBasHouseholdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service

public class ShengBasHouseholdServiceImpl extends ServiceImpl<ShengBasHouseholdMapper, ShengBasHousehold> implements ShengBasHouseholdService {
    @Autowired
    CzBasHouseholdService czBasHouseholdService;
    @DataSource(DataSourceEnum.DB3)
    @Override
    public boolean save(ShengBasHousehold entity) {
        return super.save(entity);
    }
    @DataSource(DataSourceEnum.DB3)
    @Override
    public boolean saveOrUpdate(ShengBasHousehold entity) {
        return super.saveOrUpdate(entity);
    }

    @Override
    public void toSend() {
        QueryWrapper<BasHousehold> queryWrapper = new QueryWrapper<>();
        queryWrapper.likeRight("code", 130981011).or().likeRight("code", 130903007);
        List<BasHousehold> czlist = czBasHouseholdService.list(queryWrapper);
        if (czlist != null){
            for (BasHousehold basHousehold:czlist){
                ShengBasHousehold shengBasHousehold = new ShengBasHousehold();
                shengBasHousehold.setADDTIME(basHousehold.getCreateTime());
                shengBasHousehold.setBUILDINGID(basHousehold.getCodeProvince());
                if (!StringUtils.isEmpty(basHousehold.getOriginalCode())){
                    shengBasHousehold.setCOMMUNITYID(basHousehold.getOriginalCode().substring(0,15));
                    shengBasHousehold.setHEATCOMPANY(basHousehold.getOriginalCode().substring(0,9));
                    shengBasHousehold.setHOUSEHOLDID(basHousehold.getOriginalCode());
                }
                shengBasHousehold.setCONTACTTEL(basHousehold.getContactTel());
                shengBasHousehold.setFLOOR(basHousehold.getFloor());
                shengBasHousehold.setHEATINGAREA(StringUtils.isEmpty(basHousehold.getHeatingArea())?null:Double.parseDouble(basHousehold.getHeatingArea()));
                shengBasHousehold.setHEATINGTYPE(StringUtils.isEmpty(basHousehold.getHeatingType())?null:(basHousehold.getHeatingType().equals("地暖")?1:0));
                shengBasHousehold.setHOUSEHOLDER(basHousehold.getHouseHolder());
                shengBasHousehold.setHOUSENO(basHousehold.getHouseNo());
                shengBasHousehold.setUNITNO(basHousehold.getUnitNo());
                this.save(shengBasHousehold);
            }
        }
    }
}
